package com.quanyan.assets.repository;

import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.StatusType;
import com.quanyan.assets.entity.TbAccumulateRule;
import com.quanyan.assets.entity.TbAccumulateRuleExample;
import com.quanyan.assets.entity.TbUserEggsAccumulateHistoryExample;
import com.quanyan.assets.entity.TbUserShellAccumulateHistoryExample;
import com.quanyan.assets.mapper.*;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by wmb on 2016/7/7.
 */
@Repository
public class AccumulateRuleRepository {
    @Autowired
    private TbAccumulateRuleMapper tbAccumulateRuleMapper;
    @Autowired
    private TbUserEggsAccumulateHistoryMapper tbUserEggsAccumulateHistoryMapper;
    @Autowired
    private TbUserShellAccumulateHistoryMapper tbUserShellAccumulateHistoryMapper;
    @Autowired
    private ETbUserEggsAccumulateHistoryMapper eTbUserEggsAccumulateHistoryMapper;
    @Autowired
    private ETbUserShellAccumulateHistoryMapper eTbUserShellAccumulateHistoryMapper;
    public TbAccumulateRule selectBySBA(String systemCode, String businessCode, byte assetsType) {
        TbAccumulateRuleExample tbAccumulateRuleExample=new TbAccumulateRuleExample();
        TbAccumulateRuleExample.Criteria criteria=tbAccumulateRuleExample.createCriteria();
        criteria.andSystemCodeEqualTo(systemCode).andBusinessCodeEqualTo(businessCode)
                .andAssetsTypeEqualTo(assetsType);
        List<TbAccumulateRule> tbAccumulateRuleList=tbAccumulateRuleMapper.selectByExample(tbAccumulateRuleExample);
        if(tbAccumulateRuleList.isEmpty()){
            return null;
        }else{
            return tbAccumulateRuleList.get(0);
        }
    }

    public int countUse(Integer uid, String systemCode, String businessCode, Byte assetsType) {
        if(AssetsType.ASSETS_EGGS.id.byteValue()==assetsType.byteValue()){
            //鸟蛋使用次数
            TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=new TbUserEggsAccumulateHistoryExample();
            tbUserEggsAccumulateHistoryExample.createCriteria().andSystemCodeEqualTo(systemCode)
                    .andBusinessCodeEqualTo(businessCode).andUidEqualTo(uid).andStatusEqualTo(StatusType.EFFECTIVE.id.byteValue());
            return tbUserEggsAccumulateHistoryMapper.countByExample(tbUserEggsAccumulateHistoryExample);
        }else if(AssetsType.ASSETS_SHELL.id.byteValue()==assetsType.byteValue()){
            //鸟壳使用次数
            TbUserShellAccumulateHistoryExample tbUserShellAccumulateHistoryExample=new TbUserShellAccumulateHistoryExample();
            tbUserShellAccumulateHistoryExample.createCriteria().andSystemCodeEqualTo(systemCode)
                    .andBusinessCodeEqualTo(businessCode).andUidEqualTo(uid).andStatusEqualTo(StatusType.EFFECTIVE.id.byteValue());
            return tbUserShellAccumulateHistoryMapper.countByExample(tbUserShellAccumulateHistoryExample);
        }else{
            throw new BizException(AssetsErrorEnum.ASSETS_TYPE_IS_INVALID);
        }
    }

    public int countUseToday(Integer uid, String systemCode, String businessCode, Byte assetsType) {
        if(AssetsType.ASSETS_EGGS.id.byteValue()==assetsType.byteValue()){
            //该规则今日使用次数
            return eTbUserEggsAccumulateHistoryMapper.countUseToday(uid, systemCode, businessCode);
        }else if(AssetsType.ASSETS_SHELL.id.byteValue()==assetsType.byteValue()){
            return eTbUserShellAccumulateHistoryMapper.countUseToday(uid, systemCode, businessCode);
        }else{
            throw new BizException(AssetsErrorEnum.ASSETS_TYPE_IS_INVALID);
        }

    }
}
